package com.rpoonolly.simplegame.textures;

import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TextureLoader {
	private Map<TextureFileDescriptor, TextureTileMap> loadedTextureMaps;
	
	public TextureLoader() {
		loadedTextureMaps = new ConcurrentHashMap<TextureFileDescriptor, TextureTileMap>();
	}
	
	public BufferedImage loadTexture(TextureId tileId) {
		if (!loadedTextureMaps.containsKey(tileId.textureFile))
			loadTextureFile(tileId.textureFile);
		
		TextureTileMap textureMap = loadedTextureMaps.get(tileId.textureFile);
		return textureMap.getTextureTile(tileId.tileX, tileId.tileY);
	}
	
	private void loadTextureFile(TextureFileDescriptor textureFile) {
		TextureTileMap loadedTexture = new TextureTileMap(textureFile);
		loadedTextureMaps.put(textureFile, loadedTexture);
	}
}
